//logs.js
var app = getApp()
Page({
  data: {
    id: '',
    _json_list: [],
    status: 0,
    unitl_status: '',
    ShippingMobile: null,
    Mobile: null,
  },
  onLoad: function (options) {
    console.log(options)
    var that = this
    if (options.id) { var id = options.id; that.setData({ id: id, }) }
    if (options.status) { var unitl_status = options.status; that.setData({ unitl_status: unitl_status }) }
    that.loadData(id)
  },
  onShow: function () {
    var that = this
    console.log('status:', that.data.unitl_status)
    var id = that.data.id
    var status = that.data.unitl_status

    wx.setNavigationBarTitle({
      title: '订单信息',
    })
  },
  /**
   * 加载订单数据
   * id:订单号
   */
  loadData: function (id) {
    var that = this
    wx.showToast({
      title: '信息正在更新',
      icon: 'loading',
      mask: true,
    })
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetEntity',
      data: { id: id },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var _json_list = JSON.parse(res.data.value)
          console.log('orderinfo:', _json_list)
          var num = _json_list.orderDetails.length
          var list = new Array()
          var array = _json_list.orderDetails
          if (num > 4) {
            for (var i = 0; i < array.length; i++) {
              if (i < 4) {
                list.push(array[i])
              }
            } that.setData({ orderDetails: list })
          } else {
            that.setData({ orderDetails: array, status: 1 })
          }
          if (_json_list.Address != null) {
            that.setData({ adr1: _json_list.Address })
          }
          if (_json_list.ShippingErrorMessage != null) {
            console.log('ShippingErrorMessage:', _json_list.ShippingErrorMessage)
          }
          that.setData({ money: _json_list.TotalMoney, Freight: _json_list.Freight, CreateDate: _json_list.CreateDate, OrderNumber: _json_list.OrderNumber, Mobile: _json_list.Mobile, Remark: _json_list.Remark, ShippingStatus: _json_list.ShippingStatus, ShippingUser: _json_list.ShippingUser, ShippingMobile: _json_list.ShippingMobile, ShippingMethod: _json_list.ShippingMethod, ShippingErrorMessage: _json_list.ShippingErrorMessage, RecieveName: _json_list.RecieveName })
        }
      },
      complete: function (res) {
        wx.hideToast()
      },
    })
  },
  /**
   * 加载更多
   * @post
   * 商品数大于3加载全部
   */
  loadMore: function () {
    var that = this
    wx.request({
      url: app.globalData._url + 'MenuAPI/Order/GetEntity',
      data: { id: that.data.id },
      method: 'post',
      success: function (res) {
        if (res.data.status == 1) {
          var _json_list = JSON.parse(res.data.value)
          var array = _json_list.orderDetails
          that.setData({ orderDetails: array })
        }
      }
    })
    that.setData({
      status: 1
    })
  },
  /**
   * 选择配送方式
   *  自配 / 蜂鸟
   */
  updateOrderStatus: function () {  //自配
    var orderid = this.data.id
    console.log('id:', orderid)
    wx.showModal({
      title: '温馨提示',
      content: '确定完成订单吗？',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData._url + 'MenuAPI/Order/UpdateStatus',
            data: { orderId: orderid, status: 7 },
            method: 'post',
            success: function (res) {
              console.log(res)
              if (res.data.status == 1) {
                wx.showToast({
                  title: '订单已完成',
                  icon: 'success',
                  duration: 1000,
                  mask: true,
                })
                wx.navigateBack()
              }
            }
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })

  },
  callbirdrader: function () {   //蜂鸟
    var that = this
    var orderid = that.data.id
    wx.showModal({
      title: '温馨提示',
      content: '确认呼叫蜂鸟配送',
      success: function (res) {
        if (res.cancel) {
          console.log('用户点击取消');
        } else {
          wx.request({
            url: app.globalData._url + 'MenuAPI/FengNiao/CreateOrder',
            data: { orderId: orderid },
            method: 'post',
            success: function (res) {
              if (res.data.status == 1) {
                wx.showToast({
                  title: '呼叫蜂鸟成功',
                  icon: 'success',
                  duration: 1000
                })
                that.loadData(orderid)
              } else {
                wx.showModal({
                  title: '提示',
                  content: '呼叫蜂鸟失败',
                  showCancel: false
                })
              }
              console.log(res)
            }
          })
        }
      }
    })
  },

  /**
   * 提醒修改状态
   */
  deleteOrderStatus: function () {
    var self = this
    wx.showModal({
      title: '提示',
      content: '已确定拒绝接单',
      showCancel: true,
      success: res => {
        // self.     调用修改订单状态
        console.log(res)
      }
    })
  },
  /**
   * 呼叫骑手
   */
  tocallrider: function () {
    var phone = this.data.ShippingMobile
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  /**
   * 呼叫用户
   */
  tocallUser: function () {
    var phone = this.data.Mobile
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  /**
   * 刷新订单状态
   */
  refpush: function () {
    var id = this.data.id
    console.log('id:', id)
    this.loadData(id)
  },
})
